package cn.lijiguo.concurrency.part1.ch09;

/**
 * ProduceConsumer1
 *
 * @author lijiguo
 * @date 2019/12/16 13:38
 */
public class ProduceConsumer1 {

    private int i = 0;

    private final Object LOCK = new Object();

    private void produce() {
        synchronized (LOCK) {
            System.out.println("P->" + (i++));
        }
    }

    private void consumer() {
        synchronized (LOCK) {
            System.out.println("C->" + i);
        }
    }

    public static void main(String[] args) {
        ProduceConsumer1 produceConsumer1 = new ProduceConsumer1();

        new Thread("P") {
            @Override
            public void run() {
                while (true) {
                    produceConsumer1.produce();
                }
            }
        }.start();

        new Thread("C") {
            @Override
            public void run() {
                while (true) {
                    produceConsumer1.consumer();
                }
            }
        }.start();
    }
}
